package com.android.minetopin.view.group;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.minetopin.R;
import com.android.minetopin.activity.MainRecommendType;
import com.android.minetopin.enums.EJumpStatus;
import com.android.minetopin.pojo.BannerUintPojo;
import com.android.minetopin.utils.ConstantsUrl;
import com.android.minetopin.utils.JumpUtils;
import com.android.minetopin.utils.ObjectUtils;


public class BannerItemFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private String mParam1;
    private int mParam2;
    private String mParam3;
    private View view;
    private String sortID;

    private String return_type=null;
    public static BannerItemFragment newInstance(String thumb, int photolength, String link) {
        BannerItemFragment fragment = new BannerItemFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, thumb);
        args.putInt(ARG_PARAM2, photolength);
        args.putString(ARG_PARAM3, link);
        fragment.setArguments(args);
        return fragment;
    }

    public BannerItemFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getInt(ARG_PARAM2);
            mParam3 = getArguments().getString(ARG_PARAM3);
            return_type =mParam3;
            if(!TextUtils.isEmpty(mParam3)){
                if(mParam3.startsWith("api/goods/list")){
                    String[] split = mParam3.split("/");
                    sortID = split[split.length - 1];
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        view = inflater.inflate(R.layout.item_fragment_banner, null);
        ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
        ObjectUtils.photo2(getActivity(), mParam1, iv);
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(return_type!=null) {

                    BannerUintPojo Apojo  = new BannerUintPojo();
                    Apojo.return_type = return_type;
                    Apojo.title = "";
                    if(JumpUtils.jumpToEveryWhere(getActivity(), Apojo, BannerItemFragment.this))return;

                        Intent intent = new Intent(getActivity(), MainRecommendType.class);
                    if(JumpUtils.IsRegmatch(return_type, EJumpStatus.ELottery.getRegmatch())) {
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_TITLE, "抽奖团");
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_LINK, "6");
                    }else if(JumpUtils.IsRegmatch(return_type, EJumpStatus.EnumNew.getRegmatch())) {
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_TITLE, "新专团");
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_LINK, "7");
                    }else if(JumpUtils.IsRegmatch(return_type, EJumpStatus.EnumFreePostage.getRegmatch())) {
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_TITLE, "9.9元包邮");
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_LINK, "3");
                    }else {
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_TITLE, "");
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_LINK, "-100");
                    }
                        intent.putExtra(ConstantsUrl.INTENT_KEY.return_type, Apojo.return_type);
                        startActivity(intent);


                }

//                if(!TextUtils.isEmpty(mParam3)) {
//                    if (!TextUtils.isEmpty(sortID)) {
//                        Fragment1 instent = Fragment1.getInstent();
//                        instent.setViewPager(sortID);
//                    } else {
//                        if(mParam3.startsWith("http")){
//                            Intent intent = new Intent(getActivity(), BaseObject.class);
//                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "");
//                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_URL, mParam3);
//                            startActivity(intent);
//                        }
//                    }
 //               }
            }
        });
        return view;
    }
}
